package com.fingard.dsp.bank.directbank.citic02.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directbank.citic02.CITIC02Base;
import com.fingard.io.FileHelper;
import com.fingard.net.WebRequest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;

public class FilePushUtil extends CITIC02Base {
    public void filePush(String strReq) throws IOException {
        WriteBankLogLn2("开始进行流水映射文件推送...");
        File tmpMapFile = new File(strReq);
        if (tmpMapFile.exists()) {
            JSONObject jsonRequest = new JSONObject();

            String reqBatchNo = new File(strReq).getName().split("_")[0];
            WriteBankLogLn2("当前请求批次号:" + reqBatchNo);
            jsonRequest.put("ReqSeqID", reqBatchNo);

            JSONArray jsonRequestBody = new JSONArray();

            String tmpMapContent = FileHelper.readFileText(strReq, getCharset());
            WriteBankLogLn2("当前请求批次映射文件内容:\r\n" + tmpMapContent);
            StringReader strReader = new StringReader(tmpMapContent);
            BufferedReader tmpReader = new BufferedReader(strReader);
            while (true) {
                String eachline = tmpReader.readLine();
                if (eachline == null) {
                    break;
                }
                String key = eachline.split(",")[0];
                String value = eachline.split(",")[1];
                JSONObject jsonDetail = new JSONObject();
                jsonDetail.put("AskNumber", key);
                jsonDetail.put("BankNo", value);
                jsonRequestBody.add(jsonDetail);
            }
            jsonRequest.put("RD", jsonRequestBody);
            WriteBankLogLn2("推送ATS请求报文:\r\n" + JSON.toJSONString(jsonRequest));

            String filePushUrl = bankFront.getUrl("filePush");
            WriteBankLogLn("推送ATS服务地址:" + filePushUrl);

            WebRequest tmpRequest = new WebRequest(filePushUrl);
            tmpRequest.setConnectTimeout(10000);
            tmpRequest.setRequestProperty("connection", "Close");
            tmpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            byte[] tmpWriteBytes = JSON.toJSONString(jsonRequest).getBytes(getCharset());
            String[] tmpStrRet = tmpRequest.upload(tmpWriteBytes, getCharset());
            WriteBankLogLn2("推送ATS响应报文:\r\n" + tmpStrRet[1]);
            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                String tmpRespCode = tmpRespJson.getString("Result");
                if ("success".equalsIgnoreCase(tmpRespCode)) {
                    WriteBankLogLn("流水映射文件数据推送成功");
                    bankFront.isGettingToken = true;
                } else {
                    WriteBankLogLn("流水映射文件数据推送失败");
                }
            } else {
                WriteBankLogLn("处理异常:" + tmpStrRet[1]);
            }
        } else {
            WriteBankLogLn("本机映射文件:[" + strReq + "]不存在，无需进行推送!");
        }
        WriteBankLogLn2("流水映射文件推送完毕!");
    }
}